{
    /*实现级联勾选开始*/
    var nodeCheckedSilent = false;
    function nodeChecked (event, node){
        if(nodeCheckedSilent){
            return;
        }
        nodeCheckedSilent = true;
        checkAllParent(node);
        checkAllSon(node);
        nodeCheckedSilent = false;
    }

    var nodeUncheckedSilent = false;
    function nodeUnchecked  (event, node){
        if(nodeUncheckedSilent)
            return;
        nodeUncheckedSilent = true;
        uncheckAllParent(node);
        uncheckAllSon(node);
        nodeUncheckedSilent = false;
    }

//选中全部父节点
    function checkAllParent(node){
        $('#tree').treeview('checkNode',node.nodeId,{silent:true});
        var parentNode = $('#tree').treeview('getParent',node.nodeId);
        if(!("nodeId" in parentNode)){
            return;
        }else{
            checkAllParent(parentNode);
        }
    }
//取消全部父节点
    function uncheckAllParent(node){
        $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
        var siblings = $('#tree').treeview('getSiblings', node.nodeId);
        var parentNode = $('#tree').treeview('getParent',node.nodeId);
        if(!("nodeId" in parentNode)) {
            return;
        }
        var isAllUnchecked = true;  //是否全部没选中
        for(var i in siblings){
            if(siblings[i].state.checked){
                isAllUnchecked=false;
                break;
            }
        }
        if(isAllUnchecked){
            uncheckAllParent(parentNode);
        }

    }

//级联选中所有子节点
    function checkAllSon(node){
        $('#tree').treeview('checkNode',node.nodeId,{silent:true});
        if(node.nodes!=null&&node.nodes.length>0){
            for(var i in node.nodes){
                checkAllSon(node.nodes[i]);
            }
        }
    }
//级联取消所有子节点
    function uncheckAllSon(node){
        $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
        if(node.nodes!=null&&node.nodes.length>0){
            for(var i in node.nodes){
                uncheckAllSon(node.nodes[i]);
            }
        }
    }
    /*实现级联勾选结束*/
}
function batchDel() {
    var arr = $("#roleTb").bootstrapTable("getSelections");
    console.log(arr);
    //如果没有选择提示信息
    if (arr.length == 0) {
        swal({
            type:"warning",
            title:"信息展示",
            text:"请至少选择一项删除！",
            confirmButtonText:"确定"
        });
        return;
    }
    //循环编号
    var ids = arr.map(role=>{
        return "ids="+ role.id;
    }).join("&");

    console.log(ids);
    $.get("sys/role/batchDel?"+ids,msg=>{
        //删除成功提示
        toastr.warning(msg);
        //刷新组件
        $("#roleTb").bootstrapTable("refresh");
    });
}
/*增加跳转页面*/
function add() {
    location.href = `${ctx}/sys/role/add`;
}
/*格式化日期Date*/
function fmtDate(value) {
    return value.substring(0,10);
}
/*初始化单元格并添加按钮*/
function actionFormatter() {
    var edit = `<button class="bbb btn btn-warning">修改</button> `;
    var del = `<button class="btn btn-danger aaa">删除</button>`;
    return edit+del;
}
$("#update").dialog({
    title: "修改角色",
    btnBind () {
        /*alert("发送Ajax执行修改");*/
        var param = $("#update form").serialize();
        $.post(`${ctx}/sys/role/update`,param)
            .done(msg=>{
                toastr.success(msg);
                $("#roleTb").bootstrapTable("refresh");
                $("#update").dialog("hide");
            })
    }
});
var actionEvents={
    "click .bbb":function (a,b,row) {
        $("#update [name=id]").val(row.id);
        $("#update [name=roleCode]").val(row.roleCode);
        $("#update [name=roleName]").val(row.roleName);
        $("#update").dialog("show");
    }
};
function grant() {
    $.get("sys/role/query4Tree")
    .done(data => {
        $("#tree").treeview({
            data,
            showCheckbox: true,
            levels: 3,
            multiSelect: true,
            onNodeChecked:nodeChecked,
            onNodeUnchecked:nodeUnchecked
        });
        $("#grant").dialog("show");
    });
}
$("#grant").dialog({

});

